<template>
  <div class="login01">
    <el-form :model="use" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">
      <h3>叩丁狼电商管理系统</h3>
      <el-form-item prop="username">
        <el-input v-model="use.username" placeholder="请输入账号"></el-input>
      </el-form-item>

      <el-form-item prop="pwd">
        <el-input v-model="use.pwd" placeholder="请输入密码" type="password"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button
          @click="loginFn"
          type="primary"
          style="width:100%;fontSize:16px;color:#fff;padding:12px 20px;box-sizing: border-box;"
        >登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
//引入API
import { LoginApi } from "@/request/api.js";

//引入vuex
import { mapState } from "vuex";
export default {
  data() {
    return {
      use: {},
      rules: {
        username: [
          { required: true, message: "账号不能为空", trigger: "blur" }
        ],
        pwd: [{ required: true, message: "密码不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    loginFn() {
      //请求前判断
      this.$refs.ruleForm.validate(vali => {
        if (vali) {
          LoginApi({
            username: this.use.username,
            password: this.use.pwd
          }).then(res => {
            if (res.errno === 0) {
              localStorage.setItem("token", res.data.token);
              this.$store.commit("changePer", res.data.menu);
              this.$router.push("/homepage");
            }
          })
        }else{
          return false
        }
      });
    }
  },
  computed: {
    ...mapState(["login"])
  }
};
</script>

<style lang='less' scoped>
.login01 {
  width: 100%;
  height: 100%;
  background-color: #324057;
  display: flex;
  justify-content: center;
  align-items: center;
}
.demo-ruleForm {
  width:400px;
  padding: 10px 8px;
  box-sizing: border-box;
  h3 {
    text-align: center;
    color: #fff;
    font-size: 24px;
    padding: 16px 0px;
    box-sizing: border-box;
  }
  /deep/.el-input__inner {
    font-size: 16px;
    height: 40px;
  }
}
</style>